package com.bootcamp.ex1124.exception;

import com.bootcamp.ex1124.bean.BaseRespVO;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.NoSuchElementException;

/**
 * @program: MyMavenProject
 * @description:
 * @author: Keyu Li
 * @create: 2021-11-24 14:48
 **/

@ControllerAdvice
public class UserLoginException {

    /* 处理异常，返回modelAndView，jsp文件中 exception为具体的异常类型 */
    @ExceptionHandler({BindException.class,ParameterException.class})
    public ModelAndView userNameException(Exception ex){
        ModelAndView view = new ModelAndView();
        view.setViewName("/exception.jsp");
        view.addObject("exception",ex.getMessage());
        return view;
    }

    @ResponseBody
    @ExceptionHandler(NoSuchElementException.class)
    public BaseRespVO userException2(Exception ex){
        return BaseRespVO.error(ex.getMessage());
    }

}
